package com.angelhack.metrosocial.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;

public class Utilities {

	public static final String HTPP_POST_INSERT = "http://hacktom.com/insertar_reportes.php";
	public static final String HTPP_POST_GET_COMMENT = "http://hacktom.com/peticion_mensajes.php";
	public static final String HTPP_POST_GET_IMG = "http://hacktom.com/peticion_img.php";
	public static final String HTPP_POST_PETICION_SERVER = "http://hacktom.com/peticion_server.php";
	
	public static void copyDBtoApp(Context context) {
		try {
			String destPath = Environment.getDataDirectory() + "/data/"
					+ context.getPackageName() + "/databases/";
			File f = new File(destPath + "metrosocial");
			File fir = new File(destPath);
			if (!f.exists()) {
				if (!fir.exists()) {
					fir.mkdir();
				}
				CopyDB(context.getAssets().open("metrosocial"),
						new FileOutputStream(destPath + "metrosocial"));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void CopyDB(InputStream inputStream,
			OutputStream outputStream) throws IOException {
		// ---copy 1K bytes at a time---
		byte[] buffer = new byte[1024];
		int length;
		while ((length = inputStream.read(buffer)) > 0) {
			outputStream.write(buffer, 0, length);
		}
		inputStream.close();
		outputStream.close();
	}

	public static ArrayList getDataForPost(String id_station, String user,
			String img, String coment, String status) {
		ArrayList<Object> dataValues = new ArrayList();
		dataValues.add("id_station");
		dataValues.add(id_station);

		dataValues.add("user");
		dataValues.add(user);

		dataValues.add("img");
		dataValues.add(img);

		dataValues.add("coment");
		dataValues.add(coment);

		dataValues.add("status");
		dataValues.add(status);

		dataValues.add("user");
		dataValues.add(user);

		return dataValues;

	}

	public static String getB64fromBitmap(Bitmap bitmap) {
		if (bitmap == null)
			return "";

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); // bm is the
																// bitmap object
		byte[] b = baos.toByteArray();
		return Base64.encodeToString(b, Base64.DEFAULT);
	}
	public static Bitmap decodeBase64(String input) 
	{
	    byte[] decodedByte = Base64.decode(input, 0);
	    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
	}

	public static ImageSize getImageSizeScaleTo(ImageButton imageButton) {
		DisplayMetrics displayMetrics = imageButton.getContext().getResources()
				.getDisplayMetrics();

		LayoutParams params = imageButton.getLayoutParams();
		int width = params.width; // Get layout width parameter
		if (width <= 0)
			width = getFieldValue(imageButton, "mMaxWidth"); // Check maxWidth
																// parameter
		if (width <= 0)
			width = displayMetrics.widthPixels;

		int height = params.height; // Get layout height parameter
		if (height <= 0)
			height = getFieldValue(imageButton, "mMaxHeight"); // Check
																// maxHeight
																// parameter
		if (height <= 0)
			height = displayMetrics.heightPixels;

		return new ImageSize(width, height);
	}

	public static int getFieldValue(Object object, String fieldName) {
		int value = 0;
		try {
			Field field = ImageButton.class.getDeclaredField(fieldName);
			field.setAccessible(true);
			int fieldValue = (Integer) field.get(object);
			if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) {
				value = fieldValue;
			}
		} catch (Exception e) {
		}
		return value;
	}
}
